<?php


namespace app\api\controller;


use think\cache\driver\Redis;
use think\Controller;

class OpenBooks extends Controller
{
    //书籍列表
    public function bookList(){
        $key = input("apiKey");
        $api = db("api")->where('apikey',$key)->find();
        if(empty($key) || empty($api)) return json(['code' => '8401','msg' => 'appkey_error']);
        $book_list_arr = explode("/",$api['books']);
        $book_list_str = implode(",",$book_list_arr);
        $redis = new Redis();
        $book_list = $redis->get('book_list'.':'.$key);
        if(empty($book_list)){
            $book_list = db("book")->where('id','in',$book_list_str)->select();
            $redis->set('book_list'.':'.$key,$book_list,24*3600);
        }
        $data = [];
        $total_data = [];
        foreach($book_list as $key=>$value){
            $data['bookid'] = $value['id'];
            $data['bookname'] =$value['name'];
            $data['author'] =$value['author'];
            $total_data[] = $data;
        }
        $data = ['code' => 8200,'message' => '成功','result' => $total_data];
        return json_encode($data);
    }
    //书籍详情
    public function bookInfo()
    {
        $key = input("apiKey");
        $bookid = input("bookid");
        $api = db("api")->where('apikey',$key)->find();
        if(empty($key) || empty($api)) return json(['code' => '8401','msg' => 'appkey_error']);
        $book_list_arr = explode("/",$api['books']);
        if(!in_array($bookid,$book_list_arr)) return json(['code' => '8400','msg' => 'val_error']);
        $redis = new Redis();
        $book_info = $redis->get('book_info'.':'.$key.":".$bookid);
        if(!empty($book_info)){
            $data = ['code' => 8200,'message' => '成功','result' => $book_info];
            return json_encode($data);
        }
        $bookInfo = db('book')->find($bookid);
        //总章节数
        $chapters['count'] = db('chapter')->where('book_id',$bookid)->count('id');
        $chapters['free'] = db('chapter')->where('book_id',$bookid)->where('is_free',0)->max('index');
        $cates = db('cate')->where('id',$bookInfo['id'])->find();
        $data['bookid'] = $bookInfo['id'];
        $data['bookname'] = $bookInfo['name'];
        $data['authorname'] = $bookInfo['author'];
        $data['fullflag'] = $bookInfo['complete'];
        $data['category'] = $cates['name'].','.$bookInfo['name'];
        $data['tag'] = $bookInfo['name'];
        $data['chaptercount'] = $chapters['count'];
        $data['maxfreecount'] = (int)$chapters['free'];
        $data['bookpic'] = $bookInfo['cover'];
        $data['intro'] = $bookInfo['descr'];
        $redis->set('book_info'.':'.$key.":".$bookid,$data,24*3600);
        $data = ['code' => 8200,'message' => '成功','result' => $data];
        return json_encode($data);
    }
    //章节列表、
    public function chapterList()
    {
        $keys = input("apiKey");
        $bookid = input("bookid");
        $api = db("api")->where('apikey',$keys)->find();
        if(empty($keys) || empty($api)) return json(['code' => '8401','msg' => 'appkey_error']);
        $book_list_arr = explode("/",$api['books']);
        if(!in_array($bookid,$book_list_arr)) return json(['code' => '8400','msg' => 'val_error']);
        $redis = new Redis();
       $chapter_list = $redis->get('chapter_list'.':'.$keys.':'.$bookid);
       if(!empty($chapter_list)){
            $data = ['code' => 8200,'message' => '成功','result' => $chapter_list];
            return json_encode($data);
        }
        $chapter_list = db('chapter')->where('book_id',$bookid)->select();
        /*$chapters = $redis->get('chapter_list'.':'.$bookid);
        if(empty($chapters)){
            $redis->set('chapter_list'.':'.$bookid,$chapter_list,3600);
        }*/
        $chapter = [];
        $data = [];
        foreach($chapter_list as $key=>$value){
            $chapter['volumename'] = '正文';
            $chapter['chapterorder'] = $value['index'];
            $chapter['chapterid'] = $value['id'];
            $chapter['vip'] = $value['is_free'];
            $chapter['chaptername'] = $value['name'];
            $chapter['chapterurl'] = '';
            $data[] = $chapter;
        }
        $redis->set('chapter_list'.':'.$keys.':'.$bookid,$data,24*3600);
        $data = ['code' => 8200,'message' => '成功','result' => $data];
        return json_encode($data);
    }
    //章节内容
    public function chapterInfo()
    {
        $keys = input("apiKey");
        $bookid = input("bookid");
        $chapterid = input('chapterid');
        $api = db("api")->where('apikey',$keys)->find();
        if(empty($keys) || empty($api)) return json(['code' => '8401','msg' => 'appkey_error']);
        $book_list_arr = explode("/",$api['books']);
        if(!in_array($bookid,$book_list_arr)) return json(['code' => '8400','msg' => 'val_error']);
        $bookChapter = db('chapter')->where('book_id',$bookid)->where('id',$chapterid)->find();
//        dump($bookChapter);die;
        if(empty($bookChapter)) return json(['code' => '8400','msg' => 'val_error']);
        $chapter = db('chapter')->where('id',$chapterid)->find();
        $content = db('content')->where('id',$chapter['content_id'])->find();
        $data = ['code' => 8200,'message' => '成功','result' => $content['content']];
        return json_encode($data);
    }
}